import store from './store'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import router from './router'
import { getToken } from './utils/auth'
import Cookie from 'js-cookie'
import { Notification } from 'element-ui';


NProgress.configure({ showSpinner: false }); // NProgress Configuration

router.beforeEach(async (to, from, next) => {
  NProgress.start();
  document.title = to.meta.title;
  const hasToken = getToken();
  if (hasToken) {
    if (!store.state.user.token) store.state.user = JSON.parse(Cookie.get('userInfo'));
    if (!store.state.nav.nav) store.state.nav = JSON.parse(Cookie.get('nav'));
    if (to.path === '/login') {
      next({path: '/'});
      NProgress.done();
    } else {
      if (to.meta.auth) {
        // if (store.getters.role === '管理员') next();
        // else Notification({
        //   title: '信息',
        //   message: '无权访问',
        //   type: 'info'
        // })
      } else {
        next();
      }
    }
    await store.dispatch('nav/setNav', to.meta.title.toUpperCase());
  } else {
    // next();
    if (to.path === '/login') next();
    else {
      next('/login');
    }
    NProgress.done();
  }
  next();
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})